using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using framework;

namespace MvcExtExample.Handlers
{
    public partial class logMaster_Data : System.Web.UI.Page
    {
        class logMasterAction
        {
            /// <summary>
            /// 获取日志列表
            /// </summary>
            /// <param name="strSort"></param>
            /// <param name="strField"></param>
            /// <param name="intStart"></param>
            /// <param name="intLimit"></param>
            /// <returns></returns>
            public string getLogList(string strSort, string strField, int intStart, int intLimit)
            {
                SqlConnection ts = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                ts.Open();
                String strSqlStr = "SELECT TOP 20 [ID],[OperationType],[TableName],[BusinessesName],[Content],[User_ID],[CreateTime],[IP],[Location],[Browser]FROM [Oil_Water_System].[dbo].[Tb_LogOperation]";
                SqlCommand cop = new SqlCommand(strSqlStr, ts);
                SqlDataAdapter sda = new SqlDataAdapter(cop);
                DataSet dsResult = new DataSet();
                sda.Fill(dsResult);
                DataRowView rowview = dsResult.Tables[0].DefaultView[0];    //定义一个视图
                string Privilege = rowview[0].ToString();
                JSONHelper jsonHelp = new JSONHelper();
                jsonHelp.success = true;
                jsonHelp.totlalCount = getLogListCount();
                foreach (DataRow dr in dsResult.Tables[0].Rows)
                {
                    jsonHelp.AddItem("ID", dr["ID"].ToString());
                    jsonHelp.AddItem("User_ID", dr["User_ID"].ToString());
                    jsonHelp.AddItem("OperationType", dr["OperationType"].ToString());
                    jsonHelp.AddItem("CreateTime", Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy/MM/dd"));
                    jsonHelp.AddItem("IP", dr["IP"].ToString());
                    if (dr["TableName"] != DBNull.Value)
                    {
                        jsonHelp.AddItem("TableName", dr["TableName"].ToString());
                    }
                    else
                    {
                        jsonHelp.AddItem("PRJ_STARTDATE", string.Empty);
                    }
                    if (dr["BusinessesName"] != DBNull.Value)
                    {
                        jsonHelp.AddItem("BusinessesName", dr["BusinessesName"].ToString());
                    }
                    else
                    {
                        jsonHelp.AddItem("BusinessesName", string.Empty);
                    }
                    if (dr["Content"] != DBNull.Value)
                    {
                        jsonHelp.AddItem("Content", dr["Content"].ToString());
                    }
                    else
                    {
                        jsonHelp.AddItem("Content", string.Empty);
                    }
                    if (dr["Location"] != DBNull.Value)
                    {
                        jsonHelp.AddItem("Location", dr["Location"].ToString());
                    }
                    else
                    {
                        jsonHelp.AddItem("Location", string.Empty);
                    }
                    if (dr["Browser"] != DBNull.Value)
                    {
                        jsonHelp.AddItem("Browser", dr["Browser"].ToString());
                    }
                    else
                    {
                        jsonHelp.AddItem("Browser", string.Empty);
                    }
                    jsonHelp.ItemOk();
                }
                string strResult = jsonHelp.ToString();
                return strResult;
            }

            /// <summary>
            /// 获取日志条数总数
            /// </summary>
            /// <returns></returns>
            private int getLogListCount()
            {
                SqlConnection ts = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
                ts.Open();
                string strSqlStr = "SELECT COUNT(*) FROM [dbo].[Tb_LogOperation]";
                SqlCommand cop = new SqlCommand(strSqlStr, ts);
                SqlDataAdapter sda = new SqlDataAdapter(cop);
                DataSet dsResult = new DataSet();
                sda.Fill(dsResult);
                DataRowView rowview = dsResult.Tables[0].DefaultView[0];    //定义一个视图
                return int.Parse(dsResult.Tables[0].Rows[0][0].ToString());
            }
        }

        protected string strJsonSource = "";

        logMasterAction logAction = new logMasterAction();

        protected void Page_Load(object sender, EventArgs e)
        {
            switch (Request.Params.Get("event"))
            {
                case "delete":
                    {
                        delData();
                        break;
                    }
                default:
                    {
                        showData();
                        break;
                    }
            }
        }

        private void showData()
        {
            //string tp = Request.Form["start"] + "";
            //int start = int.Parse(Request.Form["start"] + "");
            //int limit = int.Parse(Request.Form["limit"] + "");
            //string field = Request.Form["sort"] + "";
            //string sort = Request.Form["dir"] + "";
            string tp = "1";
            int start = 1;
            int limit = 20;
            string field = "";
            string sort = "";

            //List<TBL_PRJ_MZ> objList = prjAction.getPrjList(sort,field,start,limit);
            //string JsonSource = JSONHelper.ToJSON(objList); //当前页记录转成JSON格式
            //strJsonSource = @"{""totalCount"":""" + objList.Count.ToString() + "";
            //strJsonSource = strJsonSource + @""",""data"":" + JsonSource + "}"; //Grid的分页区显示所有记录数增加totalCount信息
            string JsonSource = logAction.getLogList(sort, field, start, limit);
            strJsonSource = JsonSource;

        }
        private void delData()
        {
            string delPrjID = "";
            delPrjID = Request.Params.Get("delData");
            if (!isNullOrZeroLength(delPrjID))
            {
                delPrjID = delPrjID.Substring(0, delPrjID.Length - 1);
                string[] strPrjID = delPrjID.Split(',');

                foreach (string strValue in strPrjID)
                {
                    //objInfo.PRJ_ID = strValue;
                    //objInfo.KEY.PRJ_ID = strValue;
                    //objDB.DelTBL_PRJ_M(objInfo);
                }
                //prjAction.delData(delPrjID.Split(','));
            }
            strJsonSource = @"{success: true}";
        }

        public static bool isNullOrZeroLength(string content)
        {
            if (content == null)
            {
                return true;
            }
            if (content.Trim().Length == 0)
            {
                return true;
            }
            return false;
        }
    }
}